package com.ruoyi.system.service.shopping.impl;

import com.ruoyi.system.domain.shopping.Product;
import com.ruoyi.system.domain.shopping.ShoppingCart;
import com.ruoyi.system.mapper.shopping.ShoppingCartMapper;
import com.ruoyi.system.service.shopping.ProductService;
import com.ruoyi.system.service.shopping.ShoppingCartService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private ProductService productService;
    @Override
    public List<ShoppingCart> selectList(Integer userId) {
        return shoppingCartMapper.selectList(userId);
    }

    @Override
    public Boolean add(Integer userId, Integer productId) {
        //应该还要查询商品是否存在 以及 用户是否存在
        //先查询该商品是否存在数据库中 存在就更新数量
        int i = shoppingCartMapper.selectListByUserIdAndProductId(userId, productId);
        if(i>0){
            int i1 = shoppingCartMapper.updateByUserIdAndProductId(userId, productId);
            return i1>0;
        }else {
            //不存在就新增
//            Product product = productService.selectProductById(productId);
            ShoppingCart shoppingCart = new ShoppingCart();
//            BeanUtils.copyProperties(product,shoppingCart);
            shoppingCart.setProductId(productId);
            shoppingCart.setUserId(userId);
            shoppingCart.setNum(1);
            int insert = shoppingCartMapper.insert(shoppingCart);
            return insert > 0;
        }
    }

    @Override
    public int count(Integer userId) {
        int i = shoppingCartMapper.selectProductCountByUserId(userId);
        return i;
    }

    @Override
    public Boolean updateNum(ShoppingCart shoppingCart) {
        int i = shoppingCartMapper.updateNum(shoppingCart);
        return i > 0;
    }

    @Override
    public Boolean delete(Integer shoppingCartId) {
        int i = shoppingCartMapper.deleteById(shoppingCartId);
        return i>0;
    }
}
